package com.liao.myblog.service.impl;

import com.liao.myblog.dao.BlogMapper;
import com.liao.myblog.dao.CatalogueMapper;
import com.liao.myblog.pojo.Blog;
import com.liao.myblog.pojo.Catalogue;
import com.liao.myblog.service.BlogService;
import com.liao.myblog.vo.BlogPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.support.AbstractFlashMapManager;

import java.util.Date;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private CatalogueMapper catalogueMapper;

    @Override
    public List<Blog> queryBlogList() {
        List<Blog> blogs = blogMapper.queryBlogList();
        if (null != blogs) {
            for (Blog blog : blogs) {
                Catalogue catalogue = catalogueMapper.findCatalogueById(blog.getCatalogue_id());
                blog.setCatalogue_name(catalogue.getName());
            }
            blogs.removeIf(blog -> blog.getIsDelete() == 1);
        }
        return blogs;
    }

    @Override
    public int deleteBlog(int[] ids) {
        return blogMapper.deleteBlog(ids);
    }

    @Override
    public boolean saveBlog(Blog blog) {
        int i = blogMapper.saveBlog(blog);
        return i > 0;
    }

    @Override
    public Blog queryBlogById(int id) {
        Blog blog = blogMapper.queryBlogById(id);
        Catalogue catalogueById = catalogueMapper.findCatalogueById(blog.getCatalogue_id());
        blog.setCatalogue_name(catalogueById.getName());
        return blog;
    }

    @Override
    public boolean updateBlogById(Blog blog) {
        if (blog.getIsDelete() == null) {
            blog.setIsDelete((byte) 0);
        }
        int i = blogMapper.updateBlogById(blog);
        return i > 0;
    }

    @Override
    public List<Blog> queryBlogByTitle(String title) {
        return blogMapper.queryBlogByTitle(title);
    }

    @Override
    public int findBlogCount() {
        return blogMapper.findBlogCount();
    }

    @Override
    public List<Blog> findBlogByPaging(int limit, int offset, String keyword) {
        List<Blog> blogs = blogMapper.findBlogByPaging(limit, offset, keyword);
        for (Blog blog : blogs) {
            Catalogue catalogue = catalogueMapper.findCatalogueById(blog.getCatalogue_id());
            blog.setCatalogue_name(catalogue.getName());
        }
        return blogs;
    }

    @Override
    public List<Blog> queryNewBlog(int limit) {
        return blogMapper.queryNewBlog(limit);
    }

    @Override
    public int queryBlogCountByKeyword(String keyword) {
        return blogMapper.queryBlogCountByKeyword(keyword);
    }

    @Override
    public BlogPage queryBlogByKeyword(int limit, int offset, String keyword) {
        int blogCount = blogMapper.queryBlogCountByKeyword(keyword);
        BlogPage blogPage = new BlogPage();
        blogPage.setTotal(blogCount);
        List<Blog> blogByPaging;
        System.out.println("BlogServiceImpl.queryBlogByKeyword");
        System.out.println(""+limit + "   "+offset+"    "+keyword);
        if (offset == 0) {
            blogByPaging = blogMapper.findBlogByPaging(limit, offset * limit, keyword);
        } else {
            blogByPaging = blogMapper.findBlogByPaging(limit, (offset - 1) * limit, keyword);
        }
        blogPage.setBlogs(blogByPaging);
        for (Blog blog : blogByPaging) {
            blog.setCatalogue_name(catalogueMapper.findCatalogueById(blog.getCatalogue_id()).getName());
        }
        int i = blogCount / limit;
        int i1 = blogCount % limit == 0 ? i : i + 1;
        blogPage.setPageCount(i1);
        blogPage.setPageNum(offset);
        blogPage.setCurrPage(offset);
        return blogPage;
    }

    @Override
    public List<Blog> queryHotBlog(int limit) {
        return blogMapper.queryHotBlog(limit);
    }

    @Override
    public int queryBlogCountByCatalogueId(int catalogueId) {
        return blogMapper.queryBlogCountByCatalogueId(catalogueId);
    }

    @Override
    public List<Blog> queryBlogByCatalogueId(int catalogueId, int pageNum, int pageSize) {
        List<Blog> blogs = blogMapper.queryBlogByCatalogueId(catalogueId, pageSize, pageSize * pageNum);
        for (Blog blog : blogs) {
            Catalogue catalogue = catalogueMapper.findCatalogueById(blog.getCatalogue_id());
            blog.setCatalogue_name(catalogue.getName());
        }
        return blogs;
    }
}
